package de.haw.ai3.bs.aufgabe3.a1.vonAndre;

import java.util.HashSet;
import java.util.concurrent.Semaphore;

import javax.swing.plaf.SliderUI;

public class Kasse extends Semaphore implements Comparable<Kasse> {

	private HashSet<Student> wartendeStudenten = new HashSet<>();
	
	private int nummer;

	@Override
	public String toString() {
		return "Kasse[" + nummer + "/"+wartendeStudenten.size()+"]";
	}
	
	public Kasse(int nummer) {
		super(1, true);
		this.nummer = nummer;
	}

	public void anstellen(Student student) {
		try {
			wartendeStudenten.add(student);
			acquire();
			student.sleep(5000);
			release();
			student.bezahlen(this);
		} catch (InterruptedException e) {
		} finally {
			wartendeStudenten.remove(student);
		}
	}

	public Integer getAnzahlWartendeStudenten() {
		return wartendeStudenten.size();
	}

	@Override
	public int compareTo(Kasse arg0) {
		return getAnzahlWartendeStudenten().compareTo(arg0.getAnzahlWartendeStudenten());
	}

}
